##Authors:  Roberto Carlos, Geoffrey Sheagley, Karlee Taylor
##Paper:   "Tolerance for the Free Speech of Outgroup Partisans"
##Intent: Replication Analyses 

library(readstata13)
library(tidyverse)
library(survey)
library(marginaleffects)
library(tidymodels)

#Read Data####
data_full<-read.dta13("knight_data_final_formatted.dta")
data_full$pid3<-factor(data_full$pid3,
                                       levels=c(1,2,3),
                                       labels=c("Republican", "Democrat", "Independent"))

#Experiment 1 - General Free Speech####

data_speech <- data_full %>% select(out_speech, treat_speech, treat_speech_small, college_student, 
                                    pid3, ppinc7,ppethm, ppage, ppeducat, Main_Weights)
  
data_speech$treat_speech_small<-factor(data_speech$treat_speech_small,
                              levels=c(1,2,3),
                              labels=c("Control", "Dems Hurtful", "Reps Hurtful"))
  
with(data_speech, table(treat_speech_small))
  
##Analysis 1 - Observational Design within Control Conditions#### 

data_speech_control <- data_speech %>% filter(pid3 !=6,
                                              treat_speech_small=="Control")

#No Weights & Without Controls. Reported in Main Text. 
mod_speech_nocontrol<-lm(out_speech~1+pid3,data=data_speech_control)
summary(mod_speech_nocontrol)

#Weighted & With Controls. Not reported in Main Text 
data_speech_weights<-data_speech_control %>% filter(Main_Weights !="NA")

weights_speech<-svydesign(ids=~0,
                         data=data_speech_weights,
                         weights=~Main_Weights)
weights_speech

mod_speech_control<-svyglm(out_speech~1+pid3+ppinc7+as.factor(ppethm)+ppeducat+ppage, 
                           design=weights_speech)
summary(mod_speech_control)

pred<-predictions(mod_speech_control,
                  type="link",
                  newdata=typical(pid3=c("Democrat", "Republican","Independent")))
pred<-pred %>%
  mutate(lower_ci=predicted-1.96*std.error,
         upper_ci=predicted+1.96*std.error)
pred

##Analysis 2 - Treatment by Party ID#### 

data_speech2 <- data_speech %>% filter(pid3 !=6)

mod_speech<-lm(out_speech~1+pid3*treat_speech_small, data=data_speech2)
summary(mod_speech)
  
#Post-estimation

pred<-predictions(mod_speech,
                  type="response",
                  newdata=typical(pid3=c("Democrat", "Republican","Independent"),
                                  treat_speech_small=c("Control", "Dems Hurtful", "Reps Hurtful")))
pred$treat_speech_small<-recode(pred$treat_speech_small,
                                Control="Control",
                                "Dems Hurtful" = "Democrats\nHurtful",
                                "Reps Hurtful" = "Republicans\nHurtful")


pd <- position_dodge(.2)
x<-ggplot(pred, aes(x=treat_speech_small, y=predicted, group=pid3, fill=pid3))
x+geom_errorbar(aes(ymin=conf.low, ymax=conf.high), position=pd, width=0)+
  geom_point(aes(color=pid3,fill=pid3), stat="identity", position=pd, color="black", shape=21, size=4)+
  scale_fill_brewer(palette="Set1")+
  theme(axis.title=element_text(size=12,face="bold"))+
  theme(axis.text=element_text(size=12))+
  facet_grid(~pid3)+
  scale_y_continuous(limits = c(3, 4.5))+
  xlab("Randomized Treatment")+
  ylab("Support for Free Speech")+
  theme_bw()+
  theme(legend.title=element_blank(), legend.position="none")+
  theme(axis.title=element_text(size=14,face="bold"))+
  theme(axis.text=element_text(size=12))+
  theme(strip.text.x = element_text(size = 12))

ggsave(filename="Figure1.tiff",
       device="tiff",
       width=11, 
       height=6, 
       dpi=275)  

#Outcome 2 - Free Speech on College Campus####

data_campus <- data_full %>% select(out_campus, treat_campus, treat_campus_small, college_student, 
                                    pid3, ppinc7,ppethm, ppage, ppeducat, Main_Weights)


data_campus$treat_campus_small<-factor(data_campus$treat_campus_small,
                                       levels=c(1,2,3),
                                       labels=c("Control", "Liberal\nPerspectives", "Conservative\nPerspectives"))

with(data_campus, table(treat_campus_small))

##Analysis 1 - Observational Study in Control Condition#### 

data_campus_control <- data_campus %>% filter(pid3 !=6,
                                              treat_campus_small=="Control")

mod_campus_control<-lm(out_campus~1+pid3,data=data_campus_control)
summary(mod_campus_control)

##Analysis 2 - Treatment by Party ID####

data_campus_treats <- data_campus %>% filter(pid3 !=6)

mod_campus<-lm(out_campus~1+pid3*treat_campus_small, data=data_campus_treats)
summary(mod_campus)

#Post-estimation
pred<-predictions(mod_campus,
                  type="response",
                  newdata=typical(pid3=c("Democrat", "Republican","Independent"),
                                  treat_campus_small=c("Control", "Liberal\nPerspectives", "Conservative\nPerspectives")))
pred

pd <- position_dodge(.2)
x<-ggplot(pred, aes(x=treat_campus_small, y=predicted, group=pid3, fill=pid3))
x+geom_errorbar(aes(ymin=conf.low, ymax=conf.high), position=pd, width=0)+
  geom_point(aes(color=pid3,fill=pid3), stat="identity", position=pd, color="black", shape=21, size=4)+
  scale_fill_brewer(palette="Set1")+
  theme(axis.title=element_text(size=12,face="bold"))+
  theme(axis.text=element_text(size=12))+
  scale_y_continuous(limits = c(2.5, 4.1))+
  facet_grid(~pid3)+
  xlab("Randomized Treatment")+
  ylab("Opposition to Censorship on College Campus")+
  theme_bw()+
  theme(legend.title=element_blank(), legend.position="none")+
  theme(axis.title=element_text(size=14,face="bold"))+
  theme(axis.text=element_text(size=12))+
  theme(strip.text.x = element_text(size = 12))

ggsave(filename="Figure2.tiff",
       device="tiff",
       width=11, 
       height=6, 
       dpi=275)  